﻿@{
    ViewData["Title"] = "工作列表";
    var catelist = ViewBag.catelist as IEnumerable<ArticleCate>;
}

<div class="content-top-nav">
    <h3 style="color:#444"><i class="layui-icon layui-icon-home"></i>@ViewData["Title"] </h3>
    <hr>
</div>
<div class="layui-fluid" style="margin-bottom:50px;">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <form class="layui-form" action="" lay-filter="FormSearch">
                    <div class="layui-card-body ">
                        <div class="layui-form">
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" name="title" id="title" placeholder="关键字搜索" class="layui-input" style="width:220px;">
                            </div>
                            <div class="layui-inline layui-show-xs-block" style="width:100px;">
                                <input type="checkbox" name="FullTextSwitch" value="1" lay-skin="switch" lay-text="内容|标题">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" name="Nickname" id="Nickname" placeholder="登记人" class="layui-input" style="width:100px;">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <label class="layui-form-label">日期范围</label>
                                <div class="layui-inline" id="search-range">
                                    <div class="layui-input-inline">
                                        <input type="text" id="startDate" class="layui-input" placeholder="开始日期" autocomplete="off">
                                    </div>
                                    <div class="layui-input-inline">
                                        <input type="text" id="endDate" class="layui-input" placeholder="结束日期" autocomplete="off">
                                    </div>
                                </div>
                            </div>


                            <div class="layui-inline layui-show-xs-block" style="width:120px;">
                                <select name="searchcid" id="searchcid" lay-verify="">
                                    <option value="0">所有类别</option>
                                    @foreach (var item in catelist)
                                    {
                                        @if (item.Type == 1)
                                        {
                                            <option value="@item.Id" disabled>@item.Name</option>
                                        }
                                        else
                                        {
                                            <option value="@item.Id">@item.Name</option>
                                        }

                                    }
                                </select>
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <input type="checkbox" name="MarkStatus" value="1" title="标记">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" data-type="reload" id="searchBtn"><i class="layui-icon">&#xe615;</i></button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <script type="text/html" id="toolbarAE">
                <div class="layui-btn-container">
                    @if (await Authorizer.AuthView(PermissionKeys.Article_AddOrEdit))
                    {
                                        <button class="layui-btn layui-btn-sm data-add-btn"><i class="layui-icon">&#xe642;</i>新增</button>
                    }
                </div>
            </script>

            <table class="layui-hide" id="currentTableId" lay-data="{id: 'idReload'}" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="cateUrlTpl">
                {{d.cateName}}
            </script>
            <script type="text/html" id="contentUserTpl">
                {{ d.deptName }} - {{d.nickname}}
            </script>
            <script type="text/html" id="docNumberTpl">
                第 {{d.docNumber}} 期 
            </script>
            <script type="text/html" id="contentUrlTpl">
                {{# if(d.mark!=null){ }}
                <a href="javascript:;" lay-event="markPreview"><span class="layui-badge layui-bg-blue">{{d.mark}}</span></a>
                {{# } }}
                <a lay-event="preview">{{d.title}}</a>
            </script>
            <script type="text/html" id="currentTableBar">
                
                <div class="layui-btn-group">
                    @if (await Authorizer.AuthView(PermissionKeys.Article_AddOrEdit))
                    {
                                      <a class="layui-btn layui-btn-sm " lay-event="print" title="预览"><i class="layui-icon layui-icon-print"></i></a>
                    }
                   
                {{# if( @ViewBag.userGid==1 || d.uid == @ViewBag.userId){ }}
                    @if (await Authorizer.AuthView(PermissionKeys.Article_AddOrEdit))
                    {
                                   <a class="layui-btn layui-btn-sm" lay-event="edit" title="编辑"><i class="layui-icon">&#xe642;</i></a>
                    }
                    @if (await Authorizer.AuthView(PermissionKeys.ArticleExamine_Post))
                    {
                           <a class="layui-btn layui-btn-sm  layui-btn-warm" lay-event="postExamine" title="报审"><i class="layui-icon">&#xe613;</i></a>
                    }
                {{# }}}

                    @if (ViewBag.PostCode == AdminPostConst.MASTER || ViewBag.PostCode == AdminPostConst.DEPUTY)
                    {
                    <a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="mark"  title="标记"><i class="layui-icon  layui-btn-sm layui-icon-rate"></i></a>
                    }

                    @if (await Authorizer.AuthView(PermissionKeys.Article_Delete))
                    {
                                    <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete" title="删除"><i class="layui-icon">&#xe640;</i></a>
                    }
                    else
                    {
                                        <a class="layui-btn layui-btn-sm layui-btn-disabled"><i class="layui-icon">&#xe640;</i></a>
                    }

                </div>
            </script>
        </div>
    </div>
</div>
<script>
    //console.log(@ViewBag.userId)
    layui.define(function () {
        var table = layui.table, $ = layui.jquery, layer = layui.layer, form = layui.form, laydate = layui.laydate;

        form.render();

        form.val('FormSearch', {
            searchcid: @ViewBag.cid,
        });

        laydate.render({
            elem: '#search-range' //开始时间和结束时间所在 input 框的父选择器
            //设置开始日期、日期日期的 input 选择器
            , range: ['#startDate', '#endDate'] //数组格式为 layui 2.6.6 开始新增
        });

        table.render({
            elem: '#currentTableId',
            url: '/Admin/Article/Index?cid=@ViewBag.cid&json=1',
            toolbar: '#toolbarAE',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            page: {
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
            },
            cols: [[
                { field: 'id', width: 80, title: '序号', sort: true, align: "center" },
                { field: 'cateName', width: 100, title: '类别', templet: '#cateUrlTpl' },
                { field: 'docNumber', width: 120, title: '文号', align: 'center', templet: '#docNumberTpl' },
                { field: 'title', title: '标题', templet: '#contentUrlTpl' },
                { field: 'nickname', width: 200, title: '登记人', align: 'left', templet: '#contentUserTpl' },
                { field: 'insertTime', width: 165, title: '登记时间', sort: true },
                { title: '操作', width: 300, templet: '#currentTableBar', fixed: "right", align: "left" }
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            id: 'tableReload',
            page: true
        });

        // 监听添加操作
        $(".data-add-btn").on("click", function () {
            loadMain('/Admin/Article/AddOrEdit?cid=@ViewBag.cid');
            return false;
        });
        // 监听删除操作
        $(".data-delete-btn").on("click", function () {
            var checkStatus = table.checkStatus('currentTableId'), data = checkStatus.data;
            layer.alert(JSON.stringify(data));
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                loadMain('/Admin/Article/AddOrEdit?&id=' + data.id + '&cid=' + data.cid);
                return false;
            } else if (obj.event === 'preview') {
                var index = layer.open({
                    type: 2,
                    title: ['预览内容', 'font-size:18px;'],
                    area: ['1000px', '800px'],
                    shadeClose: "true",
                    content: '/Admin/Article/AddOrEdit?preview=1&id=' + data.id
                });
            } else if (obj.event === 'print') {
                var index = layer.open({
                    type: 2,
                    title: ['预览', 'font-size:18px;'],
                    area: ['1000px', '800px'],
                    shadeClose: "true",
                    content: '/Admin/Article/AddOrEdit?print=1&id=' + data.id
                });
            } else if (obj.event === 'delete') {
                layer.confirm('删除确认？', function (index) {
                    $.post('/Admin/Article/Delete?id=' + data.id, function (res) {
                        if (res.status == 1) {
                            layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                                obj.del();
                                layer.close(index);
                            });
                        } else {
                            layer.msg(res.msg, { icon: 2, anim: 6, time: 5000 });
                        }
                    });
                });
            } else if (obj.event === 'postExamine') {
                var index = layer.open({
                    type: 2,
                    title: ['审核流转', 'font-size:18px;'],
                    area: ['1000px', '700px'],
                    shadeClose: "true",
                    content: '/Admin/Examine/PostExamineByAid?id=' + data.id
                });
                return false;
            } else if (obj.event === 'mark') {
                var index = layer.open({
                    type: 2,
                    title: ['标记', 'font-size:18px;'],
                    area: ['900px', '640px'],
                    shadeClose: "true",
                    content: '/Admin/ArticleMark/ArticleMark?aid=' + data.id//+'&title='+data.title
                });
                return false;
            } else if (obj.event === 'markPreview') {
                var index = layer.open({
                    type: 2,
                    title: ['标记详情', 'font-size:18px;'],
                    area: ['900px', '420px'],
                    shadeClose: "true",
                    content: '/Admin/ArticleMark/MarkPreview?aid=' + data.id//+'&title='+data.title
                });
            }
        });

        //搜索重载表格
        $('#searchBtn').on('click', function () {
            var field = form.val('FormSearch');
            table.reloadData('tableReload', {
                method: 'get',
                where: {
                    Title: $('#title').val(),
                    SearchCid: $('#searchcid').val(),
                    Nickname: $('#Nickname').val(),
                    StartDate: $('#startDate').val(),
                    EndDate: $('#endDate').val(),
                    MarkStatus: field.MarkStatus,
                    FullTextSwitch: field.FullTextSwitch,
                },
                page: {
                    curr: 1
                },
                page: true,
            });
            return false;
        });
    });
</script>